﻿using OnlineAuction.Business;
using OnlineAuction.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineAuction.Users
{
    public partial class EditItems : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["UserInfo"] == null)
            {
                Response.Redirect("../Home.aspx");
            }
            else if (!Page.IsPostBack)
            {
                //Get all category
                CategoryBO categoryBO = new CategoryBO();
                ddlCategoryName.DataTextField = "CategoryName";
                ddlCategoryName.DataValueField = "CategoryID";
                ddlCategoryName.DataSource = categoryBO.GetCategory();
                ddlCategoryName.DataBind();
                //Get all auction type
                AuctionTypeBO auctionTypeBO = new AuctionTypeBO();
                rbtnAuctionTypeName.DataTextField = "AuctionTypeName";
                rbtnAuctionTypeName.DataValueField = "AuctionTypeID";
                rbtnAuctionTypeName.DataSource = auctionTypeBO.GetAuctionType();
                rbtnAuctionTypeName.DataBind();
                rbtnAuctionTypeName.SelectedIndex = 0;
                //Load old details
                if (Request.QueryString["AuctionID"] != null)
                {
                    LoadDetailItemSelling(int.Parse(Request.QueryString["AuctionID"].ToString()));
                }
            }
        }

        private void LoadDetailItemSelling(int auctionID)
        {
            //Get detail item selling
            ItemBO bo = new ItemBO();
            ItemsE item = bo.DetailItemSelling(auctionID);
            //Show details
            if (item != null)
            {
                ddlCategoryName.SelectedValue = item.CategoryID.ToString();
                txtItemName.Text = item.ItemName;
                txtItemDescription.Text = item.ItemDescription;
                rbtnAuctionTypeName.SelectedValue = item.AuctionTypeID.ToString();
                txtFirstPrice.Text = item.FirstPrice.ToString();
                txtExpectationPrice.Text = item.ExpectationPrice.ToString();
                txtTimeFinishAuction.Text = item.TimeFinishAuction.ToString();
                txtBidIncrement.Text = item.BidIncrement.ToString();
                txtTimeIncrement.Text = item.TimeIncrement.ToString();
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["AuctionID"] != null)
            {
                LoadDetailItemSelling(int.Parse(Request.QueryString["AuctionID"].ToString()));
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ItemBO bo = new ItemBO();
            //string userName = "hieunt";
            //Save as image to server's folder
            string path = Server.MapPath("~/Image1/");
            fuItemImage1.SaveAs(path + fuItemImage1.FileName);
            if (fuItemImage2.HasFile == true)
            {
                fuItemImage2.SaveAs(path + fuItemImage2.FileName);
            }
            if (fuItemImage3.HasFile == true)
            {
                fuItemImage3.SaveAs(path + fuItemImage3.FileName);
            }
            if (fuItemImage4.HasFile == true)
            {
                fuItemImage4.SaveAs(path + fuItemImage4.FileName);
            }
            //create new item entity
            ItemsE item = new ItemsE(int.Parse(Request.QueryString["AuctionID"].ToString()),
                    int.Parse(ddlCategoryName.SelectedValue), txtItemName.Text,
                    "Image1/" + fuItemImage1.FileName,
                    fuItemImage2.HasFile == true ? "Image1/" + fuItemImage2.FileName : "",
                    fuItemImage3.HasFile == true ? "Image1/" + fuItemImage3.FileName : "",
                    fuItemImage4.HasFile == true ? "Image1/" + fuItemImage4.FileName : "",
                    txtItemDescription.Text);
            //Check result update item
            bool flag = bo.UpdateItems(item);
            if (flag)
            {
                Response.Redirect("ManageHome.aspx");
            }
            else
            {
                Response.Write("<script>alert('Error');</script>");
            }
        }
    }
}